/**
* 
*  Superclasse processeur
* see <a href="{@docRoot}/Documentation/processor.html"> for more information</a>
*  Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN
* @author: Urunuela Richard
* @version
*/
package storm.Processors;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import HIR.relation.Table;

import report.classSonde;
import storm.Entity;
import storm.Processors.LEATProcessor.States;
import storm.Tasks.Task;
import log.powerTrace;
/**
 * 
 * @author richard
 *
 */
public abstract class Processor extends Entity {
		public int id=0;
		private Task running=null;
		boolean powerLog=true;
		private boolean dvsSupport=false;
		int load=0;
		int idle=0;
		int InternalState=0;
		
		public enum States {
			RUNNING ,
			IDLE 

		}
		public States State= States.IDLE;
		
		double power ;
		/**
		 * puissance max pour task max consommation
		 */
		double sleep_power ;


		/**
		 * 
		 */
		public Processor() {
			super();
			
		}
		/**
		 * 
		 */
		public void init(){
			super.init();
		/*	if (this.Kernel.getAllTrace()){
			/**
			 * create file InternalState from sonde
			 */
			/*	 mySonde = new classSonde(this);
			}*/

		}
		/**
		 * 
		 * @return
		 */
		public boolean isRunning(){
			return (!(running==null));
		}
		/**
		 * 
		 * @return
		 */
		public Task getrunning() {
			// TODO Auto-generated method stub
			return this.running;
		}
		/**
		 * 
		 */
		public int getId() {
			return id;
		}
		/**
		 * 
		 */
		public void setId(int id) {
			this.id = id;
		}
		/**
		 * 
		 * @return
		 */
		public double getPower(){
			return 0;
		}
		/**
		 * 
		 */
		public void onTick(){
			if (this.isRunning()) {
				load = load + 1;
				
			}
			else {
				idle = idle + 1;
				
			}
			if (powerLog){
				powerTrace trace = new powerTrace(this.Kernel.getTime(),this.getPower(),this);
		
				trace.setLoad(load);
				trace.setIdle(idle);
				this.Kernel.getLogs().addTrace(trace);
			}
		/*	if (this.Kernel.getAllTrace()){
				this.mySonde.proceed();
				
			}*/
		}
		/**
		 * 
		 * @return
		 */
		public int getLoad(){
			return this.load;
		}
		/**
		 * 
		 * @param state2
		 */
		public void setState(int state2) {
			// TODO Auto-generated method stub
			this.InternalState = state2;
		}
		/**
		 * 
		 * @param T
		 */
		public void setRunning(Task T) {
			
			this.running = T;
			if (this.running == null) {
				this.State = Processor.States.IDLE;
			}
			else this.State = Processor.States.RUNNING;
			// TODO Auto-generated method stub
		
		}

		/**
		 * 
		 * @param dvsfactorNew
		 * @return
		 */
		public double getFreq(float dvsfactorNew) {
			// TODO Auto-generated method stub
			return 0;
		}
		/**
		 * 
		 * @return
		 */
		public int getDVSFactor() {
			// TODO Auto-generated method stub
			return 1;
		}
		/**
		 * Sonde 
		 */
		/**
		 * 
		 */
		public String SondetoString(){
			int id =0;
			String name = " ";
			if (this.isRunning()) {
				name = this.getrunning().getName();
				id = this.getrunning().id;
			}
			String res = this.getName() + "## id Task:" + id + "## name Tak:" + name  ; 
			
			return res;
		}

		/**
		 * If discre case number of discret frequency
		 * 
		 * 
		 */
		public int getNombrefreq() {
			// TODO Auto-generated method stub
			return 0;
		}
		
		public Object  clone() {
			Processor  O=null;
			O = (Processor) super.clone();
		//	System.out.println(this.getMySonde() + " ****** TEST *****" + O.getMySonde() );
			return O;
			
	
			
		}
		public Table getPowerHistoric() throws Exception {
			// TODO Auto-generated method stub
			
			ArrayList name =  new ArrayList();
			ArrayList <Class> type =  new ArrayList();
		
		
			name.add("Date");
			name.add("Power");
		    type.add(java.lang.Integer.class);
			type.add(java.lang.Double.class);
			Table res = new Table(name,type);
	
			if (this.getKernel().getLogs().getHistoPowerbyId(this.id) == null ) return res;
			
			ArrayList L= this.getKernel().getLogs().getHistoPowerbyId(this.id);

			
			Iterator i= L.iterator();
			while (i.hasNext()){
				powerTrace P = (powerTrace) i.next();
				Object  []insert = new Object[2];
				insert[0] = P.date; 
				insert[1] = P.power;
				res.add(insert);
			}
			
			
			
			
			return res;
		}


}
